﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using System.Globalization;
using System.Collections.ObjectModel;
using PhoneApp1.GeocoderService;
using Microsoft.Phone.Shell;
using System.IO.IsolatedStorage;

namespace TaxiTouchClient
{
    public partial class Client_geo_manu : PhoneApplicationPage
    {

        private const string mapKey = @"Ai8yroXkpmcBvxqn1TXtjlQhy3FJcObwJpvgxzOEDfgcvaJlOP2CCdCh585njAjA";
        private GeocodeResult location;
        private string AdresseManu;
        public Client_geo_manu()
        {
            InitializeComponent();
        }

        private void getCoordinates(String Adresse)
        {
            try
            {
                GeocodeServiceClient geocodeserviceclientadresse = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

                GeocodeRequest request = new GeocodeRequest();

                request.Options = new PhoneApp1.GeocoderService.GeocodeOptions();

                request.Options.Filters = new ObservableCollection<PhoneApp1.GeocoderService.FilterBase>();
                var filter = new PhoneApp1.GeocoderService.ConfidenceFilter();
                filter.MinimumConfidence = PhoneApp1.GeocoderService.Confidence.High;
                request.Options.Filters.Add(filter);

                request.Credentials = new PhoneApp1.GeocoderService.Credentials() { ApplicationId = mapKey };

                geocodeserviceclientadresse.GeocodeCompleted += geocodeService_GeocodeCompleted;
                request.Query = Adresse;
                geocodeserviceclientadresse.GeocodeAsync(request);

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        private void geocodeService_GeocodeCompleted(object sender, PhoneApp1.GeocoderService.GeocodeCompletedEventArgs e)
        {
            if (e.Result.Results.Count != 0)
            {
                location = e.Result.Results[0];
                IsolatedStorageSettings.ApplicationSettings["LocalisationClient"] = location;
                NavigationService.Navigate(new Uri("/Pages/Client_geo_auto.xaml", UriKind.Relative));
                //MessageBox.Show(e.Result.Results[0].Locations[0].Latitude.ToString() + " " + e.Result.Results[0].Locations[0].Longitude.ToString() + " " + AdresseManu);
            }
            else
            {
                MessageBox.Show(e.Error.Message.ToString());
            }
        }

        private void Valider_ClientGeoManu_Click(object sender, RoutedEventArgs e)
        {
            AdresseManu = textBox_adresse.Text + " " + textBox_cp.Text + " " + textBox_ville.Text;
            getCoordinates(AdresseManu);
        }
    }
}